var express = require('express');
var router = express.Router();

const graphqlHTTP = require('express-graphql');
const GraphQL = require('graphql');
let {graphql, GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLID} = GraphQL;


var MyGraphQLSchema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      hello: {
        type: GraphQLString,
        resolve() {
          return 'world';
        }
      }
    }
  })
});



router.get('/graphql', graphqlHTTP({
  schema: MyGraphQLSchema,
  graphiql: true,
  pretty: true
}));
router.post('/graphql', graphqlHTTP({
  schema: MyGraphQLSchema,
  pretty: true
}));

module.exports = router;

/////////////////////////////////////

const User = new GraphQLObjectType({
  name: 'User',
  fields: () => ({
    id: {
      type: GraphQLID
    },
    name: {
      type: GraphQLString
    }
  })
});
const Query = new GraphQLObjectType({
  name: 'Query',
  fields: () => ({
    viewer: {
      type: User,
      resolve() {
        return {
          id: '123',
          name: 'freiksenet'
        }
      }
    }
  })
});

const Schema = new GraphQLSchema({
  query: Query
});

graphql(Schema, `{
  viewer {
    id,
    name
  }
}`).then((result) => console.log(result));
